﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
using System.Text.RegularExpressions;

namespace UniRlv.Utility
{
    public static class UIHelper
    {

        public static void BindForm<T>(Form f, T t)
        {
            string[] names = EntityHelper.GetPropertyNames(t);

            foreach (string name in names)
            {
                foreach (Control item in f.Controls)
                {
                    BindingControl(item, name, t);
                }
            }


        }

        public static void ClearBinding(Form f)
        {

            foreach (Control item in f.Controls)
            {
                if (item is Panel || item is GroupBox)
                {
                    foreach (Control item2 in item.Controls)
                    {
                        item2.DataBindings.Clear();
                    }
                } 
                else
                {
                    item.DataBindings.Clear();
                }
            }

        }

        private static bool IsBindingControl(Control ctrl, string name)
        {

            if (ctrl.Name.EndsWith(name, StringComparison.OrdinalIgnoreCase))//比如 txtXXX对应属性 xXX
            {
                return true;
            }

            return false;
        }

        private static void BindingControl<T>(Control item, string name, T t)
        {
            if (item is TextBox)
            {
                if (IsBindingControl(item, name))
                    item.DataBindings.Add("Text", t, name);
            }
            else if (item is CheckBox)
            {
                if (IsBindingControl(item, name))
                    item.DataBindings.Add("Checked", t, name);
            }
            else if (item is NumericUpDown)
            {
                if (IsBindingControl(item, name))
                    item.DataBindings.Add("Value", t, name);
            }
            else if (item is DateTimePicker)
            {
                if (IsBindingControl(item, name))
                    item.DataBindings.Add("Value", t, name);
            }
            else if (item is Panel)
            {
                foreach (Control ctrl in item.Controls)
                {
                    BindingControl(ctrl, name, t);
                }
            }
            else if (item is GroupBox)
            {
                foreach (Control ctrl in item.Controls)
                {
                    BindingControl(ctrl, name, t);
                }
            }
        }

        public static void BindingComboBox2Setting(ComboBox cb, string sectionName, char splitter)
        {
            cb.Items.Clear();
            string s = ConfigurationHelper.AppGetString(sectionName);
            string[] items = s.Split(new char[] { splitter }, StringSplitOptions.RemoveEmptyEntries);
            cb.Items.AddRange(items);
        }

        public static void BindingComboBox2Setting(ComboBox cb, string sectionName, string splitter)
        {
            cb.Items.Clear();
            string s = ConfigurationHelper.AppGetString(sectionName);
            string[] items = s.Split(new string[] { splitter }, StringSplitOptions.RemoveEmptyEntries);
            cb.Items.AddRange(items);
        }

        public static void InitDataGridView(DataGridView dgv)
        {
            dgv.RowStateChanged += dgv_RowStateChanged;
            dgv.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;
            dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
            dgv.AutoGenerateColumns = false;
            dgv.MultiSelect = false;
            dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dgv.RowHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgv.MultiSelect = false;
            dgv.BackgroundColor = Color.White;
            dgv.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgv.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGray;
            dgv.RowsDefaultCellStyle.BackColor = Color.Silver;

            foreach (DataGridViewColumn col in dgv.Columns)
            {
                if (Regex.IsMatch(col.Name, "[0-9]$"))
                {//以数字结尾
                    col.DataPropertyName = col.Name.Remove(col.Name.Length-1, 1);
                }
                else
                {
                    col.DataPropertyName = col.Name;
                }
                
            }
        }

        private static void dgv_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            if (e.Row.DataGridView!=null && e.Row.DataGridView.Tag != null)
            {
                try
                {
                    int idxBase = Convert.ToInt32(e.Row.DataGridView.Tag);
                    e.Row.HeaderCell.Value = string.Format("{0}", idxBase + e.Row.Index + 1);
                }
                catch (Exception)
                {
                    e.Row.HeaderCell.Value = string.Format("{0}", e.Row.Index + 1);
                }
                
            }
            else
            {
                e.Row.HeaderCell.Value = string.Format("{0}", e.Row.Index + 1);
            }
        }
    }
}
